package com.why.hosp.controller.api;

import com.why.common.exception.YyghException;
import com.why.common.result.Result;
import com.why.common.result.ResultCodeEnum;
import com.why.common.utils.MD5;
import com.why.helper.HttpRequestHelper;
import com.why.hosp.service.DepartmentService;
import com.why.hosp.service.HospitalService;
import com.why.hosp.service.HospitalSetService;
import com.why.hosp.service.ScheduleService;
import com.why.yygh.model.hosp.Department;
import com.why.yygh.model.hosp.Schedule;
import com.why.yygh.vo.hosp.DepartmentQueryVo;
import com.why.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.Md5Crypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @program: yygh_parent
 * @description:
 * @author: @why
 * @create: 2021-06-09 08:12
 **/

@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;


    @ApiOperation(value = "上传科室")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(request.getParameterMap());
//        获取医院传递过来的经过加密的key
        String sign = (String) switchMap.remove("sign");

//        去数据库中查询上传的医院的key
        String hoscode = (String) switchMap.get("hoscode");

        String signKey = hospitalSetService.getSignKey(hoscode);
//         将数据库中查询出来的数据也进行md5加密
        signKey = MD5.encrypt(signKey);

        if (!signKey.equals(sign)) {
            return Result.fail(ResultCodeEnum.SIGN_ERROR);
        }

        departmentService.save(switchMap);


        return Result.ok();
    }


    @ApiOperation(value = "获取科室分页列表")
    @PostMapping("department/list")
    public Result department(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
//必须参数校验 略
        String hoscode = (String) paramMap.get("hoscode");
//非必填
        String depcode = (String) paramMap.get("depcode");
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String) paramMap.get("limit"));

        if (StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
//签名校验
        //        获取医院传递过来的经过加密的key
        String sign = (String) paramMap.remove("sign");
//        去数据库中查询上传的医院的key
        String signKey = hospitalSetService.getSignKey(hoscode);
//         将数据库中查询出来的数据也进行md5加密
        signKey = MD5.encrypt(signKey);

        if (!signKey.equals(sign)) {
            return Result.fail(ResultCodeEnum.SIGN_ERROR.getMessage());
        }

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        departmentQueryVo.setDepcode(depcode);
        Page<Department> pageModel = departmentService.selectPage(page, limit, departmentQueryVo);
        return Result.ok(pageModel);
    }


// TODO: 2021/6/9 签名校验

    @ApiOperation(value = "删除科室")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
//必须参数校验 略
        String hoscode = (String) paramMap.get("hoscode");
//必填
        String depcode = (String) paramMap.get("depcode");
//        if (StringUtils.isEmpty(hoscode)) {
//            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
//        }
////签名校验
//        if (!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }

        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }


    @ApiOperation(value = "上传医院")
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(request.getParameterMap());


//        获取医院传递过来的经过加密的key
        String sign = (String) switchMap.remove("sign");

//        去数据库中查询上传的医院的key
        String hoscode = (String) switchMap.get("hoscode");

        String signKey = hospitalSetService.getSignKey(hoscode);
//         将数据库中查询出来的数据也进行md5加密
        signKey = MD5.encrypt(signKey);

        if (!signKey.equals(sign)) {
            return Result.fail(ResultCodeEnum.SIGN_ERROR);
        }
        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoDataString = (String) switchMap.get("logoData");
        if (!StringUtils.isEmpty(logoDataString)) {
            String logoData = logoDataString.replaceAll("", "+");
            switchMap.put("logoData", logoData);
        }

        hospitalService.save(switchMap);
        return Result.ok();
    }


    /**
     * 去monggodb中查询 被医院管理系统调用
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "获取医院信息")
    @PostMapping("hospital/show")
    public Result hospital(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
//必须参数校验 略
        String hoscode = (String) paramMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
//签名校验
        String sign = (String) paramMap.get("sign");
        String datasign = hospitalSetService.getSignKey(hoscode);
        datasign = MD5.encrypt(datasign);
        if (!sign.equals(datasign)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
//根据编号查询信息
        return Result.ok(hospitalService.getByHoscode((String) paramMap.get("hoscode")));
    }


    //签名校验 // TODO: 2021/6/9
    @ApiOperation(value = "上传排班")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
////必须参数校验 略
//        String hoscode = (String)paramMap.get("hoscode");
//        if(StringUtils.isEmpty(hoscode)) {
//            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
//        }
////签名校验
//        if(!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }

        scheduleService.save(paramMap);
        return Result.ok();
    }


    @ApiOperation(value = "获取排班分页列表")
    @PostMapping("schedule/list")
    public Result schedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
//必须参数校验 略
        String hoscode = (String) paramMap.get("hoscode");
//非必填
        String depcode = (String) paramMap.get("depcode");
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String) paramMap.get("limit"));

        if (StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        // TODO: 2021/6/9  签名校验
////签名校验
//        if(!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        Page<Schedule> pageModel = scheduleService.selectPage(page, limit, scheduleQueryVo);
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "删除科室")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
//必须参数校验 略
        String hoscode = (String) paramMap.get("hoscode");
//必填
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        if (StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
//签名校验  // TODO: 2021/6/9
//        if(!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }

        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }


}
